package strategy

import (
	"fmt"
	"github.com/xuri/excelize/v2"
)

type ColorfulStyleStrategy struct{}

func (s *ColorfulStyleStrategy) ApplyHeaderStyle(f *excelize.File, sheet string, headers []string) error {
	styleID, _ := f.NewStyle(&excelize.Style{
		Fill:      excelize.Fill{Type: "pattern", Color: []string{"#FFA500"}, Pattern: 1},
		Font:      &excelize.Font{Bold: true, Color: "#FFFFFF"},
		Alignment: &excelize.Alignment{Horizontal: "center"},
	})
	// 设置列宽
	for i := range headers {
		col, _ := excelize.ColumnNumberToName(i + 1)
		f.SetColWidth(sheet, col, col, 20)
	}
	// 计算跨度
	endCol, err := excelize.ColumnNumberToName(len(headers))
	if err != nil {
		return err
	}
	return f.SetCellStyle(sheet, "A1", fmt.Sprintf("%s1", endCol), styleID)
}

func (s *ColorfulStyleStrategy) ApplyBodyStyle(f *excelize.File, sheet string, data [][]interface{}) error {
	// 创建一个样式，用于数据区域的单元格
	styleID, err := f.NewStyle(&excelize.Style{
		Border: []excelize.Border{
			{Type: "left", Style: 1, Color: "#000000"},
			{Type: "right", Style: 1, Color: "#000000"},
			{Type: "top", Style: 1, Color: "#000000"},
			{Type: "bottom", Style: 1, Color: "#000000"},
		},
		Fill: excelize.Fill{
			Type:    "pattern",
			Color:   []string{"#F8F8F8"}, // 灰色填充
			Pattern: 1,
		},
		Alignment: &excelize.Alignment{
			Horizontal: "left",
			Vertical:   "center",
		},
		Font: &excelize.Font{
			Color: "#333333", // 深灰色字体
		},
	})
	if err != nil {
		return err
	}

	// 计算数据区域的范围
	startRow := 2
	endCol, err := excelize.ColumnNumberToName(len(data[0]))
	if err != nil {
		return err
	}
	endRow := startRow + len(data) - 1

	// 应用样式到数据区域
	return f.SetCellStyle(sheet, fmt.Sprintf("A%d", startRow), fmt.Sprintf("%s%d", endCol, endRow), styleID)
}
